﻿@model IEnumerable<Navigation>
@{
    
    int currentNavigationId = Html.ViewContext.RouteData.Values.Get<int>("CurrentNavigationId", 0);
    bool showNavigation = Html.ViewContext.RouteData.Values.Get<bool>("ShowNavigation", true);
    long count = ViewData.Get<long>("AppCount", 0);

    User user = ViewData["User"] as User;
    bool isDomainUser = UserContext.CurrentUser != null && user.UserId == UserContext.CurrentUser.UserId;

    ApplicationModel app = ViewData["Application"] as ApplicationModel;

    IEnumerable<ApplicationManagementOperation> applicationManagementOperations = ViewData["ApplicationManagementOperations"] as IEnumerable<ApplicationManagementOperation>;
    Navigation parentNavigation = ViewData.Get<Navigation>("ParentNavigation", new Navigation());

}
<div class="tn-box tn-widget tn-widget-content tn-corner-all spb-app-panel-box">
    <div class="tn-box-content tn-widget-content tn-corner-all">
        <div class="spb-app-panel">
            <div class="spb-profile-card tn-helper-clearfix">
                <div class="spb-profile-avatar">
                    @Html.ShowUserAvatar(user, ViewData.Get<AvatarSizeType>("AvatarSizeType", AvatarSizeType.Small), navigateTarget: HyperLinkTarget._self, isShowUserCard: false)
                </div>
                <div class="tn-info">
                    <div class="spb-profile">
                        <h3 class="tn-user-name">
                            <a href="@SiteUrls.Instance().SpaceHome(user.UserId)" title="@user.DisplayName">@(StringUtility.Trim(user.DisplayName, 8, ""))</a>
                            @Html.IdentificationType(user.UserId)
                        </h3>
                    </div>
                    <div class="tn-actions">
                        @if (!isDomainUser && parentNavigation != null
                             && app != null
                             && showNavigation)
                        {
                            <a href="@parentNavigation.GetUrl(Url.SpaceKey())">
                                @(user.Profile.ThirdPerson() + "的" + parentNavigation.Text)
                            </a>
                        }
                    </div>
                </div>
            </div>
            <ul class="spb-user-atten tn-helper-clearfix">
                <li class="tn-border-gray tn-border-right"><a href="@SiteUrls.Instance().ListFollowedUsers(user.UserName)" title="@user.DisplayName">
                    <strong class="tn-count">@user.FollowedCount</strong><span>收藏</span></a></li>
                <li class="tn-border-gray tn-border-right"><a href="@SiteUrls.Instance().ListFollowers(user.UserName)" title="@user.DisplayName">
                    <strong class="tn-count">@user.FollowerCount</strong><span>粉丝</span></a></li>
                @if (app != null)
                {
                    <li><a href="@CachedUrlHelper.RouteUrl("ApplicationCount_" + app.ApplicationKey, new RouteValueDictionary { { "spaceKey", Url.SpaceKey() } })">
                        <strong class="tn-count">@(count)</strong><span>@Tunynet.Globalization.ResourceAccessor.GetString("CountObject_" + app.ApplicationKey, app.ApplicationId)
                        </span></a></li>
                }
            </ul>
            @if (applicationManagementOperations != null && applicationManagementOperations.Count() > 0 && isDomainUser)
            {
                <div class="tn-quick-action">
                    @foreach (ApplicationManagementOperation applicationManagementOperation in applicationManagementOperations)
                    {
                        if (!applicationManagementOperation.IsVisible(UserContext.CurrentUser) || applicationManagementOperation.AssociatedNavigationId > 0)
                        {
                            continue;
                        } 
                        <a class="tn-button-primary tn-button-large tn-button-text-icon-primary tn-button tn-corner-all"
                        href="@applicationManagementOperation.GetUrl(Url.SpaceKey())" target="@applicationManagementOperation.NavigationTarget">
                            @if (!string.IsNullOrEmpty(applicationManagementOperation.IconName))
                            {
                                @Html.Icon((IconTypes)Enum.Parse(typeof(IconTypes), applicationManagementOperation.IconName, false))
                            }
                            else if (!string.IsNullOrEmpty(applicationManagementOperation.ImageUrl))
                                {
                                    string imageUrl = applicationManagementOperation.ImageUrl;
                                    if (!applicationManagementOperation.ImageUrl.Trim().ToLower().StartsWith("http"))
                                    {
                                        imageUrl = SiteUrls.Instance().LogoUrl(applicationManagementOperation.ImageUrl, TenantTypeIds.Instance().User(), ImageSizeTypeKeys.Instance().Small());
                                    }   
                                    <span class="tn-icon-wrap">
                                        <img src="@imageUrl" width="16" height="16" alt="@HtmlUtility.TrimHtml(applicationManagementOperation.OperationText, 12)"/></span>
                                }
                            <span class="tn-button-text">
                                @applicationManagementOperation.OperationText</span></a>
                    }
                </div>
            }
            @if (showNavigation)
            {
                <div class="tnc-user-menu">
                    <ul class="tn-side-menu tn-border-top tn-border-gray">
                        @if (Model != null)
                        {
                            foreach (Navigation nav in Model)
                            {
                                if (!nav.IsVisible(UserContext.CurrentUser))
                                {
                                    continue;
                                }

                                string selected = string.Empty;
                                if (nav.IsCurrent(currentNavigationId))
                                {
                                    selected = "tn-border-bottom tn-border-gray tn-bg-gray tn-selected";
                                }

                            <li class="@(selected)">
                                @{
                                ApplicationManagementOperation applicationManagementOperation = applicationManagementOperations.Where(n => n.AssociatedNavigationId == nav.NavigationId).FirstOrDefault();
                                }
                                @if (!string.IsNullOrEmpty(nav.IconName))
                                {
                                    @Html.Icon((IconTypes)Enum.Parse(typeof(IconTypes), nav.IconName, false))
                                }
                                else if (!string.IsNullOrEmpty(nav.ImageUrl))
                                {
                                    string imageUrl = nav.ImageUrl;
                                    if (!nav.ImageUrl.Trim().ToLower().StartsWith("http"))
                                    {
                                        imageUrl = Tunynet.Utilities.WebUtility.ResolveUrl(nav.ImageUrl);
                                    }   
                                    <span class="tn-icon-wrap">
                                        <img src="@imageUrl" width="16" height="16" alt="@HtmlUtility.TrimHtml(nav.Text, 12)"/></span>
                                }
                                @if (applicationManagementOperation != null && applicationManagementOperation.IsVisible(UserContext.CurrentUser))
                                {
                                    <a class="tn-action" href="@applicationManagementOperation.GetUrl(Url.SpaceKey())" target="@applicationManagementOperation.NavigationTarget">
                                        <span class="tn-action-text">@applicationManagementOperation.OperationText</span></a>
                                }
                                
                                <a class="tn-menu-text" target="@(nav.NavigationTarget)" href="@nav.GetUrl(Url.SpaceKey())">@(string.Format(nav.Text, user.Profile.ThirdPerson()))</a></li>
                            }
                        }
                    </ul>
                </div>
            }
        </div>
    </div>
</div>
